// 生成随机数
const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)

// 生成随机日期
const randomDate = (start, end) => {
  return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
}

// 生成随机用户名
const randomUsername = () => {
  const names = ['admin', 'user', 'operator', 'manager', 'developer']
  const suffixes = ['001', '002', '003', '004', '005']
  return `${names[random(0, names.length - 1)]}${suffixes[random(0, suffixes.length - 1)]}`
}

// 生成随机操作类型
const randomAction = () => {
  const actions = [
    '查询集群',
    '创建节点',
    '删除实例',
    '修改配置',
    '查看监控',
    '导出数据',
    '导入数据',
    '更新状态'
  ]
  return actions[random(0, actions.length - 1)]
}

// 生成随机资源类型
const randomResource = () => {
  const resources = [
    '集群',
    '节点',
    '实例',
    '配置',
    '监控',
    '日志',
    '报表',
    '用户'
  ]
  return resources[random(0, resources.length - 1)]
}

// 生成随机状态
const randomStatus = () => {
  return Math.random() > 0.1 ? '成功' : '失败'
}

// 生成随机详情
const randomDetails = (action, resource) => {
  const details = [
    `执行${action}操作`,
    `处理${resource}数据`,
    `更新${resource}信息`,
    `查询${resource}状态`,
    `导出${resource}配置`
  ]
  return details[random(0, details.length - 1)]
}

// 生成日期数组
const generateDates = (days) => {
  const dates = []
  const now = new Date()
  for (let i = days - 1; i >= 0; i--) {
    const date = new Date(now)
    date.setDate(date.getDate() - i)
    dates.push(date.toLocaleDateString())
  }
  return dates
}

// 生成模拟数据
export const generateMockAnalyticsData = () => {
  // 生成统计数据
  const statistics = {
    totalVisits: random(1000, 5000),
    visitTrend: random(-10, 20),
    activeUsers: random(100, 500),
    userTrend: random(-5, 15),
    avgDuration: random(5, 30),
    durationTrend: random(-8, 12),
    totalActions: random(2000, 10000),
    actionTrend: random(-15, 25)
  }

  // 生成访问趋势数据
  const days = 30
  const dates = generateDates(days)
  const visitTrend = {
    dates,
    visits: dates.map(() => random(50, 200)),
    users: dates.map(() => random(20, 100))
  }

  // 生成行为分布数据
  const actionDistribution = [
    { value: random(100, 500), name: '查询操作' },
    { value: random(50, 200), name: '创建操作' },
    { value: random(30, 150), name: '删除操作' },
    { value: random(40, 180), name: '修改操作' },
    { value: random(20, 100), name: '导出操作' }
  ]

  // 生成热门操作排行数据
  const hotActions = [
    { name: '查询集群', value: random(200, 500) },
    { name: '创建节点', value: random(100, 300) },
    { name: '删除实例', value: random(50, 200) },
    { name: '修改配置', value: random(80, 250) },
    { name: '查看监控', value: random(150, 400) }
  ].sort((a, b) => b.value - a.value)

  // 生成用户活跃度数据
  const hours = Array.from({ length: 24 }, (_, i) => `${i}:00`)
  const userActivity = {
    hours,
    values: hours.map(() => random(10, 100))
  }

  // 生成行为日志数据
  const actionLogs = Array.from({ length: 50 }, () => {
    const action = randomAction()
    const resource = randomResource()
    return {
      timestamp: randomDate(new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), new Date()).toLocaleString(),
      username: randomUsername(),
      action,
      resource,
      details: randomDetails(action, resource),
      status: randomStatus()
    }
  }).sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))

  return {
    statistics,
    visitTrend,
    actionDistribution,
    hotActions,
    userActivity,
    actionLogs
  }
} 